package com.gaber.date;

import java.time.*;

/**
 * @author gaber
 * @date 2023/8/2 15:44:32
 * @description
 */
public class Java8Tester {
    public static void main(String[] args) {
        testLocalDateTime();
        testZonedDateTime();
    }
    /**
     * 本地化日期时间 API
     * */
    public static void testLocalDateTime(){
        LocalDateTime currentTime  = LocalDateTime.now();
        // 当前时间：2023-08-02T16:26:15.204
        System.out.println("当前时间：" + currentTime);
        // date1: 2023-08-02
        LocalDate date1 = currentTime.toLocalDate();
        System.out.println("date1: " + date1);

        int month = currentTime.getMonthValue(); //获取月
        int day = currentTime.getDayOfMonth(); //获取日
        int seconds = currentTime.getSecond(); //获取秒
        // 月: 8, 日: 2, 秒: 15
        System.out.println("月: " + month +", 日: " + day +", 秒: " + seconds);
        // date2: 2012-09-10T16:26:15.204
        LocalDateTime date2 = currentTime.withDayOfYear(10).withMonth(9).withYear(2012);
        System.out.println("date2: " + date2);
        // date3: 2014-12-10
        LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 10);
        System.out.println("date3: " + date3);
        // 22:15:12
        LocalTime date4 = LocalTime.of(22, 15, 12);
        System.out.println("date4: " + date4);
        // 解析字符串
        LocalTime date5 = LocalTime.parse("20:15:30");
        System.out.println("date5: " + date5);
    }

    /**
     * 使用时区的日期时间API
     * */
    public static void testZonedDateTime(){

        // 获取当前时间日期
        ZonedDateTime date1 = ZonedDateTime.parse("2015-12-03T10:15:30+05:30[Asia/Shanghai]");
        System.out.println("date1: " + date1);

        ZoneId id = ZoneId.of("Europe/Paris");
        System.out.println("ZoneId: " + id);

        ZoneId currentZone = ZoneId.systemDefault();
        System.out.println("当期时区: " + currentZone);
    }
}
